import React from 'react';
import { WeaSearchGroup, WeaFormItem, WeaLocaleProvider} from 'ecCom';
import {toJS} from "mobx"
import {WeaSwitch} from "comsMobx"
import { inject, observer} from 'mobx-react';
import { Button, Row, Col } from 'antd';
import {Text} from '../util/text';
const text = new Text();
const getLabel = WeaLocaleProvider.getLabel;

@observer
class Condition extends React.Component {

  render() {
    const {form, col, handleChange, condition,underline} = this.props;
    const {onEnterSearch} = this.props;
    const {isFormInit} = form;
    let group = [];
    const formParams = form.getFormParams();
    isFormInit && toJS(condition).map((c,index) => {
      let items = [];
      c.items = c.items || [];
      c.items.map((field, index) => {
        items.push({
          com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@z4m19b@${field.domkey[0]}`}
            viewAttr={`${field.viewAttr}`}
            label={`${field.label}`}
            labelCol={{ span: `${field.labelcol}` }}
            error={form.getError(field) }
            tipPosition="bottom"
            wrapperCol={{ span: `${field.fieldcol}` }}
            underline={underline}
            >
            {<WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@mhhgsq@${field.domkey[0]}`} fieldConfig={field} form={form} formParams={formParams} onChange={handleChange} onEnterSearch={onEnterSearch}/>}
          </WeaFormItem>),
          col: col || field.colSpan,
        })
      });
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@89f9ud@${index}`} title={c.title} needTigger={true} showGroup={c.defaultshow}  items={items} col={col}/>)
    });
    return group;
  }
}

@observer
class CardCondition extends React.Component {

  render() {
    const {form, col, handleChange, condition} = this.props;
    const {isFormInit} = form;
    let group = [];
    const formParams = form.getFormParams();
    let type = "edit";
    isFormInit && toJS(condition).map((c,index) => {
      let items = [];
      let i = 0;
      c.items = c.items || [];
      c.items.map((field, index) => {
        let col = col || field.colSpan;
        let dom;
        if (field.domkey == 'title' && field.viewAttr == '1') {
           type = "show";
        } 
        if (field.conditionType == "CASCADER") {
          i++;
          if(i == 1) {
            //   col = 4;
            //   dom = <Row>
            //   <Col span={15}>
            //     <WeaSwitch fieldConfig={field} form={form} formParams={formParams} onChange={handleChange} />
            //   </Col>
            // </Row>
            dom = <div style={{height:'45px'}}><WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@lc7f42@${field.domkey[0]}`} fieldConfig={field} form={form} formParams={formParams} onChange={handleChange} /></div>
          } else if(i>=4 && i%2 == 0) {
              col = 2;
              dom = <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@99uf0s@${field.domkey[0]}`}>
                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@65x5hg@${field.domkey[0]}`} span={12}>
                  <div style={{height:'45px'}}><WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@vx2ajq`} fieldConfig={field} form={form} formParams={formParams} onChange={handleChange} /></div>
                </Col>
              </Row>
          }else {
              col = 2;
              dom = <Row ecId={`${this && this.props && this.props.ecId || ''}_Row@bfcm7e@${field.domkey[0]}`}>
                <Col ecId={`${this && this.props && this.props.ecId || ''}_Col@xm86q1@${field.domkey[0]}`} offset={6} span={12}>
                  <div style={{height:'45px'}}><WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@if91d9`} fieldConfig={field} form={form} formParams={formParams} onChange={handleChange} /></div>
                </Col>
              </Row>
          }
          // if(i == 1) {
          //   //   col = 4;
          //   //   dom = <Row>
          //   //   <Col span={15}>
          //   //     <WeaSwitch fieldConfig={field} form={form} formParams={formParams} onChange={handleChange} />
          //   //   </Col>
          //   // </Row>
          //   col = 4;
          //     dom = <Row>
          //       <Col>
          //         <WeaSwitch fieldConfig={field} form={form} formParams={formParams} onChange={handleChange} />
          //       </Col>
          //     </Row>
          // } else if(i >=3 && i % 2 == 1 ) {
          //   items.push({
          //       com: (<Row style={{height:'55px',width:'200px'}}></Row>),
          //       col: 4,
          //     });
          //     items.push({
          //       com: (<Row style={{height:'55px'}}></Row>),
          //       col: 8,
          //     });
          //     col = 4;
          //     dom = <WeaSwitch fieldConfig={field} form={form} formParams={formParams} onChange={handleChange} />
          // }else {
          //     items.push({
          //       com: (<Row style={{height:'55px'}}></Row>),
          //       col: 8,
          //     })
          //     col = 4;
          //     dom = <WeaSwitch fieldConfig={field} form={form} formParams={formParams} onChange={handleChange} />
          // }
        } else {
          dom = <WeaSwitch ecId={`${this && this.props && this.props.ecId || ''}_WeaSwitch@y7llzp@${field.domkey[0]}`} fieldConfig={field} form={form} formParams={formParams} onChange={handleChange} />;
        }
        items.push({
          com: (<WeaFormItem ecId={`${this && this.props && this.props.ecId || ''}_WeaFormItem@go2rcj@${field.domkey[0]}`}
            viewAttr={`${field.viewAttr}`}
            label={`${field.label}`}
            labelCol={{ span: `${field.labelcol}` }}
            error={form.getError(field) }
            tipPosition="bottom"
            wrapperCol={{ span: `${field.fieldcol}` }}
            underline = {type == 'show' && field.conditionType != 'CASCADER' ? true : false}
            >
            {dom}
          </WeaFormItem>),
          col: col,
          hide: field.domkey == 'status' && field.labelcol == '0' ? true : false    //状态字段是否隐藏
        })
      });
      group.push(<WeaSearchGroup ecId={`${this && this.props && this.props.ecId || ''}_WeaSearchGroup@k9uiea@${index}`} title={c.title} needTigger={true} showGroup={c.defaultshow}  items={items} col={col}/>)
    });
    return group;
  }
}


const getAdButtons = (listStore) => {
  const {doSearch, setShowSearchAd, clearFormFields} = listStore;
  return [
    (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@glingq@doSearch`} type="primary" onClick={() => { doSearch(); setShowSearchAd(false); } }>{text.SEARCH }</Button>),
    (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@bpkppq@clearFormFields`} type="ghost" onClick={() => { clearFormFields(); } }>{text.RESET }</Button>),
    (<Button ecId={`${this && this.props && this.props.ecId || ''}_Button@g9pdgb@setShowSearchAd`} type="ghost" onClick={() => { setShowSearchAd(false) } }>{text.CANCEL }</Button>)
  ];
}

export {Condition, CardCondition, getAdButtons}
